// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        shiftWheel: {
            default:null,
            type:cc.Sprite,
            tooltip: "用于回转的轮盘精灵图像",
        },
        gameStart: {
            default:null,
            type:cc.Node,
            tooltip: "用于点击进入游戏演示的文本",
        },
        levels: [cc.Button],
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //读取存档
        this.level = cc.sys.localStorage.getItem('level');
        if (this.level === null) {
            cc.sys.localStorage.setItem('level', 0);
            this.level = 0;
        } else {
            this.level = Number(this.level);
        };
        // 通过第一关，将文本显示取消，换为关卡选择显示，并获取动画组件
        if (this.level >= 1) {
            this.gameStart.active = false;
            var levelNumber = Math.min(this.levels.length-1,this.level);
            for (var i =0;i<=levelNumber;i++) {
                this.levels[i].node.active = true;
                var animComponent = this.levels[i].getComponent(cc.Animation);
                var random = Math.random();
                var random2 = Math.random()*0.5;
                if (random >= 0.5) {
                    animComponent.play('float',random2)
                } else {
                    animComponent.play('float_reverse',random2)
                }
            };
        }
        
    },

    start () {
        AudioManager.playBgm('wait', 1);

    },

    update (dt) {
        //镜像轮盘刷新
        if (this.shiftWheel.type == cc.Sprite.Type.FILLED) {
            this.shiftWheel.fillStart += 0.05 * dt;
            if (this.shiftWheel.fillStart >= 1) {
                this.shiftWheel.fillStart -= 1;
            }
        };
    },

    enterLevel (target,data) {
        cc.director.loadScene("level-"+data);
    },

    endGame () {
        cc.game.end();
    },
});
